package com.traffic.fuwu.service.impl;

import java.util.List;
import java.util.Map;

import com.traffic.fuwu.dao.AccountMapper;
import com.traffic.fuwu.dao.AlipayEnterpriseAccountMapper;
import com.traffic.fuwu.dao.SftpMapper;
import com.traffic.fuwu.mybatis.pojo.*;
import com.traffic.fuwu.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.traffic.fuwu.dao.CompanyMapper;
import com.traffic.fuwu.service.CompanyService;

@Service
public class CompanyServiceImpl implements CompanyService {
    @Autowired
    private CompanyMapper companyMapper;
    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private SftpMapper sftpMapper;
    @Autowired
    private AlipayEnterpriseAccountMapper alipayEnterpriseAccountMapper;

    @Override
    public List<Company> getFindCompany(String comName, String comAddress, String appKey, int startRow, int pageSize) {
        try {
            List<Company> list = companyMapper.getFindCompany(comName, comAddress, appKey, startRow, pageSize);
            if (list != null && list.size() != 0) {
                return list;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int getFindCompanyCount(String comName, String comAddress, String appKey) {
        try {
            int count = companyMapper.getFindCompanyCount(comName, comAddress, appKey);
            if (count != 0) {
                return count;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<Map<String, Object>> getFindCom(String comName, String comAddress, String comCode, int startRow, int pageSize) {
        try {
            List<Map<String, Object>> list = companyMapper.getFindCom(comName, comAddress, comCode, startRow, pageSize);
            if (list != null && list.size() != 0) {
                return list;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int getFindComCount(String comName, String comAddress, String comCode) {
        try {
            int count = companyMapper.getFindComCount(comName, comAddress, comCode);
            if (count != 0) {
                return count;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public Map<String, Object> getById(int comId) {
        try {
            List<Map<String, Object>> list = companyMapper.getById(comId);
            if (list != null && list.size() != 0) {
                Map<String, Object> temp = list.get(0);
                return temp;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void addCompany(Company company, Sftp sftp, Account account,AlipayEnterpriseAccount alipayEnterpriseAccount) {
        if (account != null) {
            accountMapper.insert(account);
        }
        if (company != null) {
            companyMapper.insert(company);
        }
        if (sftp != null) {
            sftpMapper.insert(sftp);
        }
        if(alipayEnterpriseAccount != null){
            alipayEnterpriseAccountMapper.insert(alipayEnterpriseAccount);
        }
    }

    @Override
    public Company getUpdateById(int comId) {
        CompanyExample example = new CompanyExample();
        example.or().andComIdEqualTo(comId);
        try {
            List<Company> list = companyMapper.selectByExample(example);
            if (list != null && list.size() != 0) {
                Company company = list.get(0);
                return company;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void updateCompany(Company company) {
        companyMapper.updateByPrimaryKey(company);
    }

    @Override
    public void updateSftp(Sftp sftp) {
        sftpMapper.updateByPrimaryKey(sftp);
    }

    @Override
    public void updateAccount(Account account) {
        accountMapper.updateByPrimaryKey(account);
    }

    @Override
    public void deleteCompany(int comId) {
        companyMapper.deleteByPrimaryKey(comId);
    }

    @Override
    public void deleteSftp(int sftpId) {
        sftpMapper.deleteByPrimaryKey(sftpId);
    }

    @Override
    public void deleteAccount(int accountId) {
        accountMapper.deleteByPrimaryKey(accountId);
    }

}
